from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
import time
class Base:
    list=[]
    def __init__(self,driver):
        self.driver=driver
        #self.drivers=webdriver.Edge()

    def implicitly_wait(self,x):
        self.driver.implicitly_wait(x)

    def shot(self):
        t = time.strftime("%Y%m%d-%H%M%S", time.localtime())
        self.driver.save_screenshot(t + '.png')

    def get_url(self,url):
        self.driver.get(url)

    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver:self.driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            self.shot()
            print(e)

    def find_elements(self,loc):
        try:
            Base.list=self.driver.find_elements(*loc)
            return Base.list
        except Exception as e:
            print(e)
    #点击
    def click(self,loc):
        try:
            self.find_element(loc).click()
        except Exception as e:
            self.shot()
            print(f"点击操作失败，失败原因是：{e}")
    #清除
    def clear(self,loc):
        try:
            self.find_element(loc).clear()
        except Exception as e:
            self.shot()
            print(f"清除操作失败，失败原因是：{e}")
    #输入
    def send_keys(self,loc,info):
        try:
            self.find_element(loc).send_keys(info)
        except Exception as e:
            self.shot()
            print(f"输入操作失败，失败原因是：{e}")
    #获取文本
    def text(self,loc):
        try:
            return self.find_element(loc).text
        except Exception as e:
            self.shot()
            print(f"获取文本失败，失败原因是：{e}")
    #滚动条
    def scroll(self, loc,x):
        try:
            return self.driver.execute_script(f'window.scrollTo(0,{x})')
        except Exception as e:
            self.shot()
            print(f"滚动操作失败，失败原因是：{e}")

    #滚动条
    def enter(self, loc):
        try:
            return self.find_element(loc).send_keys(Keys.ENTER)
        except Exception as e:
            self.shot()
            print(f"回车操作失败，失败原因是：{e}")



